<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/09/21 20:51:19 $)
// $RCSfile: functions_contacts.php,v $ : $Revision: 1.5 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Insert a new Contact
*/
function insertContact($contacttype, $args)
{
	global $dbCore, $_SWIFT;

	extract($args);

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."contacts` (`contacttype`, `staffid`, `fullname`, `jobtitle`, `initials`, `calendarcategoryid`, `email1address`, `email2address`, `email3address`, `customerid`, `mobiletelephonenumber`, `cartelephonenumber`, `pagernumber`, `othertelephonenumber`, `imaddress`, `companyname`, `department`, `officelocation`, `businessaddress`, `businesstelephonenumber`, `business2telephonenumber`, `businessfaxnumber`, `businesshomepage`, `homeaddress`, `hometelephonenumber`, `home2telephonenumber`, `homefaxnumber`, `birthdaydateline`, `anniversarydateline`, `mailingaddress`, `otheraddress`, `webpage`, `assistantname`, `assistanttelephonenumber`, `lastupdate`) VALUES ('". $dbCore->escape($contacttype) ."', '". intval($_SWIFT["staff"]["staffid"]) ."', '". $dbCore->escape($fullname) ."', '". $dbCore->escape($jobtitle) ."', '". $dbCore->escape($initials) ."', '". intval($calendarcategoryid) ."', '". $dbCore->escape($email1address) ."', '". $dbCore->escape($email2address) ."', '". $dbCore->escape($email3address) ."', '". $dbCore->escape($customerid) ."', '". $dbCore->escape($mobiletelephonenumber) ."', '". $dbCore->escape($cartelephonenumber) ."', '". $dbCore->escape($pagernumber) ."', '". $dbCore->escape($othertelephonenumber) ."', '". $dbCore->escape($imaddress) ."', '". $dbCore->escape($companyname) ."', '". $dbCore->escape($department) ."', '". $dbCore->escape($officelocation) ."', '". $dbCore->escape($businessaddress) ."', '". $dbCore->escape($businesstelephonenumber) ."', '". $dbCore->escape($business2telephonenumber) ."', '". $dbCore->escape($businessfaxnumber) ."', '". $dbCore->escape($businesshomepage) ."', '". $dbCore->escape($homeaddress) ."', '". $dbCore->escape($hometelephonenumber) ."', '". $dbCore->escape($home2telephonenumber) ."', '". $dbCore->escape($homefaxnumber) ."', '". intval($birthdaydateline) ."', '". intval($anniversarydateline) ."', '". $dbCore->escape($mailingaddress) ."', '". $dbCore->escape($otheraddress) ."', '". $dbCore->escape($webpage) ."', '". $dbCore->escape($assistantname) ."', '". $dbCore->escape($assistanttelephonenumber) ."', '". DATENOW ."');");
	$contactid = $dbCore->lastInsertId();
	if (!$contactid)
	{
		return false;
	}

	$dbCore->query("INSERT INTO `". TABLE_PREFIX ."calendardata` (`datatype`, `typeid`, `contents`) VALUES ('". TEAMWORK_CONTACT ."', '". intval($contactid) ."', '". $dbCore->escape($body) ."');");

	return $contactid;
}

/**
* Update a Contact
*/
function updateContact($contactid, $contacttype, $args)
{
	global $dbCore, $_SWIFT;

	extract($args);

	$dbCore->query("UPDATE `". TABLE_PREFIX ."contacts` SET `contacttype` = '". $dbCore->escape($contacttype) ."', `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."', `fullname` = '". $dbCore->escape($fullname) ."', `jobtitle` = '". $dbCore->escape($jobtitle) ."', `initials` = '". $dbCore->escape($initials) ."', `calendarcategoryid` = '". intval($calendarcategoryid) ."', `email1address` = '". $dbCore->escape($email1address) ."', `email2address` = '". $dbCore->escape($email2address) ."', `email3address` = '". $dbCore->escape($email3address) ."', `customerid` = '". $dbCore->escape($customerid) ."', `mobiletelephonenumber` = '". $dbCore->escape($mobiletelephonenumber) ."', `cartelephonenumber` = '". $dbCore->escape($cartelephonenumber) ."', `pagernumber` = '". $dbCore->escape($pagernumber) ."', `othertelephonenumber` = '". $dbCore->escape($othertelephonenumber) ."', `imaddress` = '". $dbCore->escape($imaddress) ."', `companyname` = '". $dbCore->escape($companyname) ."', `department` = '". $dbCore->escape($department) ."', `officelocation` = '". $dbCore->escape($officeloation) ."', `businessaddress` = '". $dbCore->escape($businessaddress) ."', `businesstelephonenumber` = '". $dbCore->escape($businesstelephonenumber) ."', `business2telephonenumber` = '". $dbCore->escape($business2telephonenumber) ."', `businessfaxnumber` = '". $dbCore->escape($businessfaxnumber) ."', `businesshomepage` = '". $dbCore->escape($businesshomepage) ."', `homeaddress` = '". $dbCore->escape($homeaddress) ."', `hometelephonenumber` = '". $dbCore->escape($hometelephonenumber) ."', `home2telephonenumber` = '". $dbCore->escape($home2telephonenumber) ."', `homefaxnumber` = '". $dbCore->escape($homefaxnumber) ."', `birthdaydateline` = '". intval($birthdaydateline) ."', `anniversarydateline` = '". intval($anniversarydateline) ."', `mailingaddress` = '". $dbCore->escape($mailingaddress) ."', `otheraddress` = '". $dbCore->escape($otheraddress) ."', `webpage` = '". $dbCore->escape($webpage) ."', `assistantname` = '". $dbCore->escape($assistantname) ."', `assistanttelephonenumber` = '". $dbCore->escape($assistanttelephonenumber) ."', `lastupdate` = '". DATENOW ."' WHERE `contactid` = '". intval($contactid) ."';");

	$dbCore->query("UPDATE `". TABLE_PREFIX ."calendardata` SET `contents` = '". $dbCore->escape($body) ."' WHERE `datatype` = '". TEAMWORK_CONTACT ."' AND `typeid` = '". intval($contactid) ."';");

	return true;
}

/**
* Delete Contacts
*/
function deleteContacts($contactidlist, $guidid = 0)
{
	global $dbCore, $_SWIFT;

	if (!_is_array($contactidlist))
	{
		return false;
	}

	$_contactidlist = array();
	$dbCore->query("SELECT `contactid` FROM `". TABLE_PREFIX ."contacts` WHERE ((`contacttype` = '". SWIFTPUBLIC ."') OR (`contacttype` = '". SWIFTPRIVATE ."' AND `staffid` = '". intval($_SWIFT["staff"]["staffid"]) ."')) AND `contactid` IN (". buildIN($contactidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_contactidlist[] = $dbCore->Record["contactid"];
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."contacts` WHERE `contactid` IN (". buildIN($_contactidlist) .");");
	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendardata` WHERE `datatype` = '". TEAMWORK_CONTACT ."' AND `typeid` IN (". buildIN($_contactidlist) .");");

	// First load the links
	$_insertsql = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". LINK_CONTACT ."' AND `typeid` IN (". buildIN($_contactidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_insertsql[] = "('". TEAMWORK_CONTACT ."', '". $dbCore->escape($dbCore->Record["entryid"]) ."', '". DATENOW ."', '". intval($guidid) ."')";
	}

	if (count($_insertsql))
	{
		$dbCore->query("INSERT INTO `". TABLE_PREFIX ."twdeletelog` (`entrytype`, `entryid`, `dateline`, `guidid`) VALUES ". implode(",", $_insertsql).";");
	}

	$dbCore->query("DELETE FROM `". TABLE_PREFIX ."calendarlinks` WHERE `linktype` = '". LINK_CONTACT ."' AND `typeid` IN (". buildIN($_contactidlist) .");");

	return true;
}

/**
* Convert Contact Post to Arguments
*/
function contactPostToArguments()
{
	$args = array("fullname" => $_POST["fullname"], "jobtitle" => $_POST["jobtitle"], "initials" => $_POST["initials"], "calendarcategoryid" => $_POST["calendarcategoryid"], "email1address" => $_POST["email1address"], "email2address" => $_POST["email2address"], "email3address" => $_POST["email3address"], "customerid" => $_POST["customerid"], "mobiletelephonenumber" => $_POST["mobiletelephonenumber"], "cartelephonenumber" => $_POST["cartelephonenumber"], "pagernumber" => $_POST["pagernumber"], "othertelephonenumber" => $_POST["othertelephonenumber"], "imaddress" => $_POST["imaddress"], "companyname" => $_POST["companyname"], "department" => $_POST["department"], "officelocation" => $_POST["officelocation"], "businessaddress" => $_POST["businessaddress"], "businesstelephonenumber" => $_POST["businesstelephonenumber"], "business2telephonenumber" => $_POST["business2telephonenumber"], "businessfaxnumber" => $_POST["businessfaxnumber"], "businesshomepage" => $_POST["businesshomepage"], "homeaddress" => $_POST["homeaddress"], "hometelephonenumber" => $_POST["hometelephonenumber"], "home2telephonenumber" => $_POST["home2telephonenumber"], "homefaxnumber" => $_POST["homefaxnumber"], "birthdaydateline" => iif(!empty($_POST["birthdaydateline"]), calenderDateToDateline($_POST["birthdaydateline"])), "anniversarydateline" => iif(!empty($_POST["anniversarydateline"]), calenderDateToDateline($_POST["anniversarydateline"])), "mailingaddress" => $_POST["mailingaddress"], "otheraddress" => $_POST["otheraddress"], "webpage" => $_POST["webpage"], "assistantname" => $_POST["assistantname"], "assistanttelephonenumber" => $_POST["assistanttelephonenumber"], "body" => $_POST["body"]);

	return $args;
}
?>